<?php
/**
 * @author guanqun@
 * 短连接生成类
 *
 */
class ml_biz_guang_hash2redis {
    private $redis;
    public function __construct($conf) {
        $this->redis = new Redis ();
        $this->redis->connect ( $conf['host'], $conf['port'], 1 );
    }
    public function addtoredis($content) {
        // 逛美物
        $this->redis->zAdd ( 'gf_guang', $content ['hotrank'], $content ['rid'] );
        // 时间
        $this->redis->set ( 'gf_time_' . $content ['rid'], strtotime ( $content ['ctime'] ) );
        // 品类
        $this->redis->zAdd ( 'gf_c_' . $content ['gd_catelog'], $content ['hotrank'], $content ['rid'] );
        // 标签
        $tags = explode ( ',', $content ['gd_tag'] );
        foreach ( $tags as $tag ) {
            //$tagid = $this->redis->get ( 'tag2id_' . ml_tool_resid::str_hash ( $tag ) );
            $tagid=ml_tool_resid::str_hash ( $tag );
            $this->redis->zAdd ( 'gf_t_' . $tagid, $content ['hotrank'], $content ['rid'] );
        }
        // 价格
        $this->redis->zAdd ( 'gf_pr' . $this->_getprice ( $content['gd_catelog'] ,$content ['gd_price'] ), $content ['hotrank'], $content ['rid'] );
        // 颜色
        if ($content ['color']) {
            $this->redis->zAdd ( 'gf_co' . $content ['color'], $content ['hotrank'], $content ['rid'] );
        }
        //性别
        $this->redis->zAdd ( 'gf_sex' . $content ['sex'], $content ['hotrank'], $content ['rid'] );
    }
    public function delredis($content) {
        // 逛美物
        $this->redis->zRem ( 'gf_guang', $content ['rid'] );
        // 时间
        $this->redis->delete ( 'gf_time_' . $content ['rid'] );
        // 品类
        $this->redis->zRem ( 'gf_c_' . $content ['gd_catelog'], $content ['rid'] );
            // 标签
        $tags = explode ( ',', $content ['gd_tag'] );
        foreach ( $tags as $tag ) {
            $tagid = $this->redis->get ( 'tag2id_' . ml_tool_resid::str_hash( $tag ) );
            $this->redis->zRem ( 'gf_t_' . $tagid, $content ['rid'] );
        }
        // 价格

        $this->redis->zRem ( 'gf_pr' . $this->_getprice ( $content['gd_catelog'] , $content ['gd_price'] ), $content ['rid'] );
        // 颜色
        if ($content ['color']) {
            $this->redis->zRem ( 'gf_co' . $content ['color'], $content ['rid'] );
        }
        //性别
        $this->redis->zAdd ( 'gf_sex' . $content ['sex'], $content ['hotrank'], $content ['rid'] );
    }

    private function _getprice($ctg , $price) {
        $priceMap = ml_factory::load_standard_conf('priceMap');
        $priceMap = $priceMap[0];
        foreach ($priceMap as $key => $value) {
            if($price > $value['min'] and $price < $value['max'])
            {
                return $key;
            }
        }
        return 0;
    }
}

